<?php
/** An interface to the Hosting Reborn API hosting account functions.
 * @package com\hostingreborn\api
 * @link https//api.hostingreborn.com/docs/functions/hostingaccount/
 *
 * @author      Jon Cram <jon@hostingreborn.com>
 * @copyright   https://api.hostingreborn.com/code/copyright/
 * @license     https://api.hostingreborn.com/code/license/
 * @version     1.0
 *
 * $Date$
 * $Revision$
 *
 *
 */

require_once('HR_api.class.php');

class HR_hostingaccountApi extends HR_api {

    /***************************************************************************/
    /* Attributes                                                              */
    /***************************************************************************/

    private $hostingAccountCollectionProperties = array(
            '/hostingaccount/domain' => 'domain',
            '/hostingaccount/username' => 'username',
            '/hostingaccount/owner' => 'owner',
            '/hostingaccount/creationtime' => 'creationtime',
            '/hostingaccount/serverip' => 'serverip',
            '/hostingaccount/storagelimit' => 'storagelimit',
            '/hostingaccount/bandwidthlimit' => 'bandwidthlimit',
            '/hostingaccount/storageusage' => 'storageusage',
            '/hostingaccount/bandwidthusage' => 'bandwidthusage',
            '/hostingaccount/storagecost' => 'storagecost',
            '/hostingaccount/bandwidthcost' => 'bandwidthcost',
            '/hostingaccount/status' => 'status',
            '/hostingaccount/suspendedby' => 'suspendedby',
            '/hostingaccount/suspensioncategory' => 'suspensioncategory',
            '/hostingaccount/suspensionreason' => 'suspensionreason'
    );

    /***************************************************************************/
    /* Constructors                                                            */
    /***************************************************************************/

    /***************************************************************************/
    /* Public methods                                                          */
    /***************************************************************************/


    /**
     * @link http://api.hostingreborn.com/docs/functions/hostingaccount/
     *
     * @param string $key
     * @param string $domain
     * @param string $email
     * @return array
     */
    public function get($key = NULL, $domain = NULL) {
        /*
            Exceptions thrown by API:
                - database (handled internally by API)
                - system (handled internally by API)
        */

        $fullArgumentList = func_get_args();

        $standardArguments = array(
                'key' => $key,
                'domain' => $domain
        );

        $additionalArguments = array(
                'excludeDeleted' => 'true'
        );

        $queryString = $this->buildQueryString($standardArguments, $fullArgumentList, $additionalArguments);
        
        return $this->read('hostingaccount/get/?'.$queryString)->getCollection($this->hostingAccountCollectionProperties);
    }
    
    
    public function getNameservers($key = NULL, $domain = NULL) {
        /*
            Exceptions thrown by API:
                - database (handled internally by API)
                - system (handled internally by API)
        */
        
        $fullArgumentList = func_get_args();

        $standardArguments = array(
                'key' => $key,
                'domain' => $domain
        );

        $queryString = $this->buildQueryString($standardArguments, $fullArgumentList);
        
        return $this->read('hostingaccount/get_nameservers/?'.$queryString)->getSetValues('/nameservers/nameserver');
    }


    /**
     * http://api.hostingreborn.com/docs/functions/hostingaccount/reset_password/
     *
     * @param string $key
     * @param string $domain
     * @param string $new_password Optional, a secure password will be generated if omitted
     * @return string
     */
    public function resetPassword($key = NULL, $domain = NULL, $new_password = NULL) {
        /*
            Exceptions thrown by API:
                - database (handled internally by API)
                - system (handled internally by API)
                - hostingaccount
                    200: password reset failed
                    201: new password not sufficiently strong
                    301: new password not sufficiently strong
        */

        $arguments = array(
                'key' => $key,
                'domain' => $domain,
                'new_password' => $new_password
        );

        try {
            return $this->write('hostingaccount/reset_password/', $arguments)->getValue('/newpassword');
        } catch (HR_apiResponseUnhandledException $e) {
            $this->handleApiResponseUnhandledException($e);
        }
    }


    /**
     * @link http://api.hostingreborn.com/docs/functions/hostingaccount/set_bandwidth_limit/
     *
     * @param string $key
     * @param string $domain
     * @param int $limit
     * @return bool
     */
    public function setBandwidthLimit($key = NULL, $domain = NULL, $limit = NULL) {
        /*
            Exceptions thrown by API:
                - database (handled internally by API)
                - system (handled internally by API)
        */

        $fullArgumentList = func_get_args();

        $standardArguments = array(
                'key' => $key,
                'domain' => $domain,
                'limit' => $limit
        );

        $arguments = $this->buildArguments($standardArguments, $fullArgumentList);

        return $this->write('hostingaccount/set_bandwidth_limit/', $arguments)->wasSuccessful();
    }


    /**
     * @link http://api.hostingreborn.com/docs/functions/hostingaccount/set_storage_limit/
     *
     * @param string $key
     * @param string $domain
     * @param int $limit
     * @return bool
     */
    public function setStorageLimit($key = NULL, $domain = NULL, $limit = NULL) {
        /*
            Exceptions thrown by API:
                - database (handled internally by API)
                - system (handled internally by API)
        */

        $fullArgumentList = func_get_args();

        $standardArguments = array(
                'key' => $key,
                'domain' => $domain,
                'limit' => $limit
        );

        $arguments = $this->buildArguments($standardArguments, $fullArgumentList);

        return $this->write('hostingaccount/set_storage_limit/', $arguments)->wasSuccessful();
    }


    /**
     * @link http://api.hostingreborn.com/docs/functions/hostingaccount/suspend/
     *
     * @param string $key
     * @param string $domain
     * @param string $reason
     * @return bool
     */
    public function suspend($key = NULL, $domain = NULL, $reason = NULL) {
        /*
            Exceptions thrown by API:
                - database (handled internally by API)
                - system (handled internally by API)
        */

        $fullArgumentList = func_get_args();

        $standardArguments = array(
                'key' => $key,
                'domain' => $domain,
                'reason' => stripslashes($reason)
        );

        $arguments = $this->buildArguments($standardArguments, $fullArgumentList);

        return $this->write('hostingaccount/suspend/', $arguments)->wasSuccessful();
    }


    /**
     * @link http://api.hostingreborn.com/docs/functions/hostingaccount/unsuspend/
     *
     * @param string $key
     * @param string $domain
     * @return bool
     */
    public function unsuspend($key = NULL, $domain = NULL) {
        /*
            Exceptions thrown by API:
                - database (handled internally by API)
                - system (handled internally by API)
        */


        $fullArgumentList = func_get_args();

        $standardArguments = array(
                'key' => $key,
                'domain' => $domain
        );

        $arguments = $this->buildArguments($standardArguments, $fullArgumentList);

        return $this->write('hostingaccount/unsuspend/', $arguments)->wasSuccessful();
    }


    /**
     *  @link http://api.hostingreborn.com/docs/functions/hostingaccount/terminate/
     *
     * @param string $key
     * @param string $domain
     * @return bool
     */
    public function terminate($key = NULL, $domain = NULL) {
        /*
            Exceptions thrown by API:
                - database (handled internally by API)
                - system (handled internally by API)
        */

        $fullArgumentList = func_get_args();

        $standardArguments = array(
                'key' => $key,
                'domain' => $domain
        );

        $arguments = $this->buildArguments($standardArguments, $fullArgumentList);

        return $this->write('hostingaccount/terminate/', $arguments)->wasSuccessful();
    }

    
    /**
     * @link http://api.hostingreborn.com/docs/functions/hostingaccounts/add/
     *
     * @param string $key
     * @param string $domain
     * @param string $username
     * @param int $storageLimit
     * @param int $bandwidthLimit
     * @param bool $useExternalDns
     * @return array
     */
    public function add($key = NULL, $domain = NULL, $username = NULL, $storageLimit = NULL, $bandwidthLimit = NULL, $useExternalDns = NULL) {
        /*
            Exceptions thrown by API:
                - database (handled internally by API)
                - system (handled internally by API)
                - service
                  - 100: User name invalid
                  - 101: Domain name invalid
                  - 102: Domain name taken
                  - 103: User name taken
                  - 104: No working servers found
                  - 105: All servers have reached their storage limits
        */

        $arguments = array(
                'key' => $key,
                'domain' => $domain,
                'username' => $username,
                'storage_limit' => $storageLimit,
                'bandwidth_limit' => $bandwidthLimit,
                'use_external_dns' => $useExternalDns
        );

        $collectionProperties = array(
                '/add_hosting_account_result/service_creation/password' => 'password',
                '/add_hosting_account_result/storage_limit/status' => array(
                        'index' => 'storageLimitResult',
                        'type' => 'bool'
                ),
                '/add_hosting_account_result/storage_limit/value' => 'storageLimitSet',
                '/add_hosting_account_result/bandwidth_limit/status' => array(
                        'index' => 'bandwidthLimitResult',
                        'type' => 'bool'
                ),
                '/add_hosting_account_result/bandwidth_limit/value' => 'bandwidthLimitSet'
        );

        try {
            return $this->write('hostingaccounts/add/', $arguments)->getCollection($collectionProperties);
        } catch (HR_apiResponseUnhandledException $e) {
            $this->handleApiResponseUnhandledException($e);
        }

    }


    /**
     * @link http://api.hostingreborn.com/docs/functions/hostingaccounts/list/
     *
     * @param string $key
     * @param bool $excludeDeleted
     * @return array
     */
    public function getList($key = NULL, $excludeDeleted = true) {
        /*
            Exceptions thrown by API:
                - database (handled internally by API)
                - system (handled internally by API)
                - access (invalid key, handled internally by API)
        */

        $fullArgumentList = func_get_args();

        $standardArguments = array(
                'key' => $key,
                'exclude_deleted' => ($excludeDeleted === true) ? 'true' : 'false'
        );

        $queryString = $this->buildQueryString($standardArguments, $fullArgumentList);

        $hostingAccounts = $this->read('hostingaccounts/list/?'.$queryString)->getSet('/hostingaccounts/hostingaccount');
        foreach ($hostingAccounts as $index => $hostingAccount) {
            $hostingAccounts[$index] = $hostingAccount->getCollection($this->hostingAccountCollectionProperties);
        }

        return $hostingAccounts;
    }

    /***************************************************************************/
    /* Private methods                                                         */
    /***************************************************************************/

    /**
     *
     * @throws HR_apiHostingAccountException
     * @param HR_apiResponseUnhandledException $exception
     */
    private function handleApiResponseUnhandledException(HR_apiResponseUnhandledException $exception) {
        switch ($exception->getApiExceptionType()) {
            case 'hostingaccount':
                switch ($exception->getApiExceptionCode()) {
                    case '100':
                        $exceptionMessage = 'User name invalid';
                        break;

                    case '101':
                        $exceptionMessage = 'Domain name invalid';
                        break;

                    case '102':
                        $exceptionMessage = 'Domain name taken';
                        break;

                    case '103':
                        $exceptionMessage = 'User name taken';
                        break;

                    case '104':
                        $exceptionMessage = 'No working servers found';
                        break;

                    case '105':
                        $exceptionMessage = 'All servers have reached their storage limits';
                        break;
                    
                    case '200':
                        $exceptionMessage = 'password not sufficiently strong';
                        break;                    

                    case '201':
                        $exceptionMessage = 'action failed as hosting account is suspended';
                        break;
                    
                    case '202':
                        $exceptionMessage = 'action failed as hosting account is suspended';
                        break;                    


                }

                throw new HR_apiHostingAccountException($exceptionMessage, $exception->getApiExceptionCode());

                break;

            case 'account':
                switch ($exception->getApiExceptionCode()) {
                    case '101':
                        $exceptionMessage = 'action disallowed - account overdrawn';
                        break;
                }

                throw new HR_apiAccountException($exceptionMessage, $exception->getApiExceptionCode());

                break;
        }       

    }

}

?>